﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Follow.Business;
using System.Windows.Forms;

namespace Follow.Cadastros
{
    public partial class Meta : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["idUsuario"] == null)
            {
                Response.Redirect("../Default.aspx");
            }
            else
            {
                Common.ValidaPermissoes valida = new Common.ValidaPermissoes();
                bool possuiPermissao = valida.validaPermissaoPorTela("Meta", Convert.ToInt32(Session["idGrupoUsuario"]));

                if (possuiPermissao == false)
                {
                    Response.Redirect("../permissaoNegada.aspx");
                }

                if (Request["Msg"] != null)
                {
                    divMsg1.Visible = true;
                    divMsg1.Attributes["class"] = "isa_info";
                    lblMsg1.Text = Request["Msg"].ToString();

                }
                this.PopulaMetas();
            }
        }

        private void PopulaMetas()
        {
            MetaBO bo = new MetaBO();
            gvMetas.DataSource = bo.findByDescricao(txtPesquisa.Text);
            gvMetas.DataBind();
        }
   
        protected void btnPesquisa_Click(object sender, EventArgs e)
        {
           this.PopulaMetas();
        }

        protected void btnIncluir_Click(object sender, EventArgs e)
        {
            txtNome.Text = "";
            IDMeta.Value = "";
            mvMetas.ActiveViewIndex = 1;
            divMsg2.Visible = false;
        }

        protected void gvMetas_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            Model.Meta meta = new Model.Meta();
            try
            {
                MetaBO bo = new MetaBO();
                if (IDMeta.Value != "")
                {
                    meta.IDMeta = Int32.Parse(IDMeta.Value);
                }
                meta.Nome = txtNome.Text;
                meta.Tipo = ddlTipoMeta.SelectedItem.Value;
                this.PopulaMetas();
                mvMetas.ActiveViewIndex = 0;
                bo.save(meta, Convert.ToInt32(Session["idUsuario"]));
                divMsg1.Visible = true;
                divMsg1.Attributes["class"] = "isa_success";
                lblMsg1.Text = "Meta salva com sucesso!";
            }
            catch (Exception ex)
            {
                divMsg2.Visible = true;
                if (ex.Message.IndexOf("duplicate") > -1)
                {
                    divMsg2.Attributes["class"] = "isa_warning";
                    lblMsg2.Text = "Meta já cadastrada no sistema";
                }
                else
                {
                    divMsg2.Attributes["class"] = "isa_info";
                    lblMsg2.Text = "Erro: " + ex.Message;
                }
            }
        }

        protected void btnVoltar_Click(object sender, EventArgs e)
        {
            mvMetas.ActiveViewIndex = 0;
            divMsg1.Visible = false;
        }

        protected void gvMetas_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Editar"))
            {
                Int32 id = Convert.ToInt32(this.gvMetas.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                if (id != null)
                {
                    if (id > 0)
                    {
                        MetaBO bo = new MetaBO();
                        Model.Meta Meta = bo.load(id);
                        ddlTipoMeta.SelectedValue = Meta.Tipo;
                        txtNome.Text = Meta.Nome;
                        IDMeta.Value = Meta.IDMeta.ToString();
                        lblTitulo2.Text = "Editar Meta";
                        mvMetas.ActiveViewIndex = 1;
                        divMsg2.Visible = false;
                    }
                }
            }

            else if (e.CommandName.Equals("Excluir"))
            {
                if (System.Windows.Forms.MessageBox.Show("Tem certeza que deseja excluir?", "Excluir", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification) ==
                    System.Windows.Forms.DialogResult.OK)
                {
                    divMsg1.Visible = true;
                    Int32 id = 0;
                    try
                    {
                        id = Convert.ToInt32(this.gvMetas.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    }
                    catch (Exception ex)
                    {
                        divMsg1.Attributes["class"] = "isa_error";
                        lblMsg1.Text = "Erro: " + ex.Message;
                        return;
                    }
                    MetaBO bo = new MetaBO();
                    try
                    {
                        bo.delete(id, Convert.ToInt32(Session["idUsuario"]));
                        divMsg1.Attributes["class"] = "isa_success";
                        lblMsg1.Text = "Meta excluída com sucesso!";
                        PopulaMetas();
                    }
                    catch (Exception ex)
                    {
                        divMsg1.Attributes["class"] = "isa_error";
                        lblMsg1.Text = "Erro: " + ex.Message;
                    }
                }
            }
        }
    }
}